package com.ebt.m.users;

import android.content.Context;
import android.content.Intent;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;

import com.ebt.m.R;
import com.ebt.m.commons.buscomponent.listview.BaseListView;
import com.ebt.m.commons.buscomponent.listview.ISectionFactory;
import com.ebt.m.commons.buscomponent.listview.IView;
import com.ebt.m.commons.widgets.BaseRxActivity;
import com.ebt.m.commons.widgets.view.EBTDialog;
import com.ebt.m.users.bean.CardService;
import com.jakewharton.rxbinding2.view.RxView;

import java.util.concurrent.TimeUnit;

/**
 * Created by wanglu on 2018/2/7.
 */

public class CardServiceContent extends BaseListView<CardServicePresenter> {
    public CardServiceContent(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public CardServiceContent(Context context) {
        super(context);
    }

    public CardServiceContent(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    private IView.OnClickListner listner = new IView.OnClickListner() {
        @Override
        public void onClick(View view, Object... args) {
            if (view.getId() == R.id.action_delete) {
                CardService cardService = (CardService) args[1];
                EBTDialog ebtDialog = new EBTDialog();
                ebtDialog.setMessage("确认删除该增员相册吗？");
                ebtDialog.setLeftButton("取消", v -> ebtDialog.dismiss());
                ebtDialog.setRightButton("确认", v -> mPresenter.deleteItem(cardService));
                ebtDialog.show(((BaseRxActivity) getContext()).getSupportFragmentManager(), this.getClass().getSimpleName());
            }
        }

        @Override
        public void onLongClick(View view, Object... args) {

        }
    };

    @Override
    protected ISectionFactory createViewFactory() {
        return viewType -> {

            if (viewType == CardServicePresenter.TYPE_ADD) {
                return new CardServiceAddView(getContext());
            } else if (viewType == CardServicePresenter.TYPE_SERVICE) {
                CardServiceItemView cardServiceItemView = new CardServiceItemView(getContext());
                cardServiceItemView.setOnClickViewListener(listner);
                return cardServiceItemView;
            }
            return null;
        };
    }

    @Override
    protected CardServicePresenter createPresenter() {
        return new CardServicePresenter(getContext(), this);
    }

    @Override
    public void loadNext() {

    }

    @Override
    public void update(Object... args) {
        super.update(args);
        mPresenter.loadNew(args);
    }

    @Override
    protected int setListviewColor() {
        return getResources().getColor(R.color.common_page_bg);
    }


    @Override
    protected View configEmptyView() {
        View emptyContent = LayoutInflater.from(getContext()).inflate(R.layout.cardservice_list_empty, null);
        Button btn_new = (Button) emptyContent.findViewById(R.id.btn_empty);
        RxView.clicks(btn_new)
                .throttleFirst(500, TimeUnit.MILLISECONDS)
                .subscribe(aVoid -> {
                    Intent intent = new Intent(getContext(), CardServiceEditActivity.class);
                    getContext().startActivity(intent);
                });
        return emptyContent;
    }
}
